<?php

namespace app\home\controller;

use think\Controller;
use think\Request;

class Login extends Controller
{
   
    public function index()
    {
        $this->view->engine->layout(false);
        return view();
    }


    public function register(){
        $this->view->engine->layout(false);
        return view();
    }
    //手机号注册
    public function phone(){
        //接受数据
        $params=input();
        //参数检测
        $validate = $this->validate($params, [
            'phone|手机号' => 'require|regex:1[3-9]\d{9}|unique:user,phone',
            
            'code|验证码' => 'require|length:4',
            'password|密码' => 'require|length:6,20|confirm:repassword',
        ]);

        if($validate !== true){
            $this->error($validate);
        }

        $params['password'] = $params['password'];
        $params['username'] = $params['phone'];

        // $params['nickname'] = encrypt_phone($params['phone']);
        \app\common\model\User::create($params,true);
        $this->redirect('home/login/index');



    }
    public function dologin(){
        $params=input();
        $validate=$this->validate($params,[
           
            'username'=>'require',
            'password'=>'require'

        ]);
        if($validate !=true){
            $this->error($validate);
        }
        $password=$params['password'];
        // $info =\app\common\model\User::where('phone',$params['username'])->whereOr('email',$params['username'])
        // ->where('password',$password)->find();
        //
        $info =\app\common\model\User::where(function($query)use($params){
            $query->where('phone',$params['username'])->whereOr('email',$params['username']);
        })->where('password',$password)->find();



        if($info){
            session('user_info',$info->toArray());
            session('nickname',$params['username']);
            // session('phone',$params['phone']);
            //从session取跳转地址
            $back_url=session('back_url')?:'home/index/index';

$this->redirect('home/index/index');

        }else{
            $this->error('用户名或密码错误');

        }

    }
    public function logout(){
        session(null);
        //页面跳转
        $this->redirect('home/login/index');
    }

    
}
